package com.neutech.solar.entity;

import com.neutech.solar.constant.Constant;
import com.neutech.solar.util.ImageUtil;

import java.awt.*;

public class Planet extends Star{

    public Star star;


    public Planet() {}

    public Planet(Star star,String imgkey,double AU,double e,int T,String name) {
        this.name = name;
        this.star = star;
        this.img = ImageUtil.maps.get(imgkey);
        this.longAxis = getLongAxisByAU(AU);
        this.shortAxis = getShortAxisByAU(e);
        this.height = img.getHeight(null);
        this.width = img.getWidth(null);
        this.x = Constant.WINDOW_WIDTH / 2 + longAxis;
        this.y = Constant.WINDOW_HEIGHT / 2;
        this.speed = getSpeedByT(T);
        this.theta = 0.0;
    }

    public Planet(Star star,String imgName,double AU,double e,int T,String name,boolean smallPlanet,boolean Venus ) {
        this(star,imgName,AU,e,T,name);
        this.smallPlanet = smallPlanet;
        this.Venus = Venus;
    }

    private int getLongAxisByAU(double AU) {
        return (int)(100 * AU);
    }
    private int getShortAxisByAU(double e) {
        return (int)(this.longAxis * Math.sqrt(1 - e * e)) / 2;
    }
    private double getSpeedByT(int t) {
        return 0.1 * 365 / t;
    }


    @Override
    public void draw(Graphics g) {
        Color c = g.getColor();
        Font f = g.getFont();
        Font font = new Font("幼圆",Font.BOLD,22);
        g.setFont(font);
        g.setColor(Color.white);
        if (!smallPlanet){
            drawTrace(g);
        }
        g.drawString(name,x,y - 30);
        g.setFont(f);
        g.setColor(c);

        g.drawImage(img,x,y,null);
        move();
    }


    public void drawTrace(Graphics g) {
        int x = (int)(this.star.x - this.longAxis - this.star.width + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
        int y = this.star.y - this.shortAxis + this.star.height / 2;
        int width = this.longAxis * 2;
        int height = this.shortAxis * 2;
        g.drawOval(x,y,width,height);
    }

    @Override
    public void move() {
        if (Venus){
            x = (int)((longAxis * Math.cos(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
            y = (int)(shortAxis * Math.sin(theta)) + star.y + star.height / 2 - this.height / 2;
        } else {
            x = (int)((longAxis * Math.sin(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
            y = (int)(shortAxis * Math.cos(theta)) + star.y + star.height / 2 - this.height / 2;
        }

        theta += speed;
    }
}
